<?php
/**
*
* 版权所有：寻之网络<www.findz.cn>
* 作    者：释<272067516@qq.com>
* 日    期：2016-01-24
* 版    本：1.0.0
* 功能说明：菜单控制器。
*
**/

namespace Admin\Controller;
use Admin\Controller\ComController;
class MenuController extends BaseController {
    public function index(){
		$p = isset($_GET['p'])?intval($_GET['p']):'1';

		$m = M('nav');
		$pagesize = 10;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		$count = $m->count();
		
		$list  = $m->order('sort asc')->select();
		$list = $this->getMenu($list);
		
		$page	=	new \Think\Page($count,$pagesize); 
        $this->assign('list',$list);	

		$this -> display();
    }
	
	public function del(){
		
		$id = isset($_REQUEST['id'])?$_REQUEST['id']:false;

		if(M('nav')->where(array('id'=>$id))->delete()){
			
			$this->success('恭喜，菜单删除成功！');
		}else{
			$this->error('参数错误！');
		}
	}
	
	public function edit($id=0){
		$id = intval($id);
		$m = M('nav');
		$currentmenu = $m->where("id='$id'")->find();
		if(!$currentmenu) {
			$this->error('参数错误！');
		}
		
		$option = $m->order('sort ASC')->select();
		$option = $this->getMenu($option);
		$this->assign('option',$option);
		$this->assign('currentmenu',$currentmenu);
		$this -> display();
	}
	
	public function update(){
        if($_POST){
		$id = I('post.id','','intval');
        //var_dump($id);exit;
		$data['name'] = I('post.name','','strip_tags');
        $data['url'] = I('post.url','','strip_tags');
        $data['status'] = I('post.status','','intval');
		$data['sort'] = I('post.sort','','intval');
		if($id){
			M('nav')->where("id='{$id}'")->save($data);
		}else{
			M('nav')->add($data);
		}
		$this->success('操作成功！',U('menu/index'));
        }
	}
	
	public function add(){
		$option = M('nav')->order('sort ASC')->select();
		$option = $this->getMenu($option);
        $this->assign('option',$option);
		$this -> display();
	}
}